package com.daka.service.impl;

import com.daka.dao.OrganizationGroupVerifyMapper;
import com.daka.dao.PlatformGroupVolunteerMapper;
import com.daka.entity.GroupVolunteer;
import com.daka.entity.OrganizationGroupVerify;
import com.daka.entity.PlatformGroupVolunteer;
import com.daka.service.GroupService;
import com.daka.service.GroupVolunteerService;
import com.daka.service.OrganizationGroupVerifyService;
import com.daka.vo.db.OrgGroupVerifyVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author beliveli on 2016/3/31 0031.
 */
@Service("organizationGroupVerifyService")
@SuppressWarnings("unused")
public class OrganizationGroupVerifyServiceImpl implements OrganizationGroupVerifyService {

    @Resource(name = "organizationGroupVerifyMapper")
    private OrganizationGroupVerifyMapper organizationGroupVerifyMapper;

    @Resource
    private PlatformGroupVolunteerMapper platformGroupVolunteerMapper;

    @Resource
    private GroupVolunteerService groupVolunteerService;

    @Resource
    private GroupService groupService;

    @Override
    public List<OrgGroupVerifyVo> selectByParentCode(String parentCode) {
        return organizationGroupVerifyMapper.selectByParentCode(parentCode);
    }

    @Override
    public int updateById(OrganizationGroupVerify organizationGroupVerify) {
        return organizationGroupVerifyMapper.updateById(organizationGroupVerify);
    }

    public int verifySuccess(OrganizationGroupVerify organizationGroupVerify) {

        organizationGroupVerify.setVerifyTime(new Timestamp(System.currentTimeMillis()));
        int resultCount = this.updateById(organizationGroupVerify);

        organizationGroupVerify = organizationGroupVerifyMapper.selectById(organizationGroupVerify.getId());

        groupService.updatePlatform(organizationGroupVerify.getPlatformId(),organizationGroupVerify.getGroupId(),organizationGroupVerify.getParentCode());

        if (resultCount == 1) {

            List<GroupVolunteer> groupVolunteerList = groupVolunteerService.selectByGroup(organizationGroupVerify.getGroupId(), 2);//2:已审核通过加入

            if (groupVolunteerList.size() > 0) {
                //拼接list
                List<PlatformGroupVolunteer> platformGroupVolunteerList = PlatformGroupVolunteer.build(organizationGroupVerify.getGroupId(), organizationGroupVerify.getPlatformId(), groupVolunteerList);

                resultCount = platformGroupVolunteerMapper.insertBatch(platformGroupVolunteerList);

                if (resultCount <= 0) {
                    return -2;//保存用户信息失败
                }
            }
            return 1;
        } else {
            return -1;//标识修改失败
        }
    }
}
